﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace Searchingdfa
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(!string.IsNullOrEmpty(text_selpath.Text))
            {
                string param1 = this.Seacontent.Text;
                if (string.IsNullOrEmpty(param1)) return;

                ComboBoxItem selItem = com_extcombobox.SelectedItem as ComboBoxItem;
                string ext = selItem.Content.ToString();
                if (string.IsNullOrEmpty(ext)) return;
                string IsSear = "";
                Searching(param1, ext, text_selpath.Text, out IsSear);
            }
        }

        private void Searching(string param1, string fileext, string path1, out string IsSear)
        {
            IsSear = "";
            if (Directory.Exists(path1))
            {
                string[] filess1 = Directory.GetFiles(path1, "*."+fileext);
                foreach(string path2 in filess1)
                {
                    if (System.IO.Path.GetExtension(path2) == "." + fileext)
                    {
                        //是指定格式的文件，打开
                        if (SearchContent(path2, param1))
                        {
                            IsSear = path2;
                            return;
                        }

                    }
                }

                string[] folder1= Directory.GetDirectories(path1);
                foreach(string path3 in folder1)
                {
                    Searching(param1, fileext, path3, out IsSear);
                    if (!string.IsNullOrEmpty(IsSear))
                        return;
                }

            }
        }

        private bool SearchContent(string filepath, string parma1)
        {
            using (StreamReader sr = new StreamReader(filepath))
            {
                bool IsCheck = false;
                string linestr;
                while((linestr = sr.ReadLine()) != null)
                {
                    if (linestr.ToLower().Contains(parma1.ToLower()))
                    {
                        IsCheck = true;
                        break;
                    }
                }
                if(IsCheck)
                {
                    StringBuilder sb = new StringBuilder();
                    while ((linestr = sr.ReadLine()) != null)
                    {
                        sb.Append(linestr);
                        sb.Append("\r\n");
                    }
                    textblock1.Text = sb.ToString();
                    targetFile.Text = filepath;
                }
            }

             return false;
        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Interop.HwndSource source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;
            System.Windows.Forms.IWin32Window win = new Searchingdfa.OldWindow(source.Handle);
            System.Windows.Forms.DialogResult result = dlg.ShowDialog(win);
            text_selpath.Text = dlg.SelectedPath;

            if (!string.IsNullOrEmpty(text_selpath.Text))
            {
                Button button1 = (Button)sender;
                button1.Content = ".....";
            }
        }

        private void text_extlist_TextChanged(object sender, TextChangedEventArgs e)
        {
            com_extcombobox.Items.Clear();
            string extlist = text_extlist.Text;
            string[] extlistes = extlist.Split(new char[] { ';' });
            for (int i = 0; i < extlistes.Length; i++)
            {
                if (string.IsNullOrEmpty(extlistes[i])) continue;
                ComboBoxItem cItem = new ComboBoxItem();
                cItem.Content = extlistes[i];
                if ("dfa" == extlistes[i])
                {
                    cItem.IsSelected = true;
                }
                com_extcombobox.Items.Add(cItem);
            }
        }
    }

    public class OldWindow : System.Windows.Forms.IWin32Window
    {
        IntPtr _handle;
        public OldWindow(IntPtr handle)
        {
            _handle = handle;
        }
        #region IWin32Window Members
        IntPtr System.Windows.Forms.IWin32Window.Handle
        {
            get { return _handle; }
        }
        #endregion
    }
}
